package com.xxx.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxx.demo.dto.ShowAllDto;
import com.xxx.demo.entity.ShowEntity;
import com.xxx.demo.entity.ShowType;
import com.xxx.demo.mapper.ShowTypeMapper;
import com.xxx.demo.service.ShowService;
import com.xxx.demo.mapper.ShowMapper;
import com.xxx.demo.vo.ShowAllVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 */
@Service
public class ShowServiceImpl extends ServiceImpl<ShowMapper, ShowEntity>
    implements ShowService {
    @Autowired(required = false)
    private ShowMapper showMapper;
    @Autowired(required = false)
    private ShowTypeMapper showTypeMapper;

    /**
     * 分页查询所有展示（管理）
     * @param pageNo
     * @param pageSize
     * @param showAllDto
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public IPage<ShowAllVo> findShowAllPage(Integer pageNo, Integer pageSize, ShowAllDto showAllDto){
        Page<ShowAllVo> page = new Page<>(pageNo,pageSize);
        return showMapper.findShowAllPage(page,showAllDto);
    }

    /**
     * 管理后台新增展示类型以及展示物品
     * @param showAllDto
     * @return
     */
    @Override
    @Transactional
    public int addShowAll(ShowAllDto showAllDto){
        if (ObjectUtil.isEmpty(showAllDto)){
            return 0;
        }
        ShowEntity showEntity = new ShowEntity();
        BeanUtil.copyProperties(showAllDto,showEntity);
        showEntity.setCreationDate(new Date());
        int insert = showMapper.insert(showEntity);
        return insert;
    }

    /**
     * 根据id查询
     * @param showId
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public ShowAllVo findShowAllById(Long showId){
        ShowAllVo showAllVo = new ShowAllVo();
        ShowEntity showEntity = showMapper.selectOne(new QueryWrapper<ShowEntity>().eq("id", showId).eq("delete_flag", 0));
        BeanUtil.copyProperties(showEntity,showAllVo);
        ShowType showType = showTypeMapper.selectById(showEntity.getShowTypeId());
        if (ObjectUtil.isNotEmpty(showType)){
            showAllVo.setShowType(showType.getShowType());
        }
        return showAllVo;
    }

    /**
     * 根据id修改展示
     * @param showAllDto
     * @return
     */
    @Override
    @Transactional
    public int updateShowAll(ShowAllDto showAllDto){
        log.error("修改展示入参：{}" + showAllDto);
        ShowEntity showEntity = showMapper.selectById(showAllDto.getId());
        if (ObjectUtil.isEmpty(showEntity)){
            //return 0;
            throw new RuntimeException("修改失败！");
        }
        if (StrUtil.isEmpty(showAllDto.getShowPhotoUrl()) && StrUtil.isNotEmpty(showEntity.getShowPhotoUrl())){
            showAllDto.setShowPhotoUrl(showEntity.getShowPhotoUrl());
        }
        BeanUtil.copyProperties(showAllDto,showEntity);
        int i = showMapper.updateById(showEntity);
        return i;
    }

    /**
     * 删除展示
     * @param id
     * @return
     */
    @Override
    @Transactional
    public int deleteShowById(Long id){
        ShowEntity showEntity = showMapper.selectById(id);
        if (ObjectUtil.isEmpty(showEntity)){
            return 0;
        }
        showEntity.setDeleteFlag(1);
        int i = showMapper.updateById(showEntity);
        return i;
    }

    /**
     * 查询部分展示数据
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public List<ShowAllVo> selectSomeShow(){
        return showMapper.selectSomeShow();
    }

    /**
     * 根据id查询展示
     * @param showId
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public ShowAllVo findShowById(Long showId){
        return showMapper.findShowById(showId);
    }

    /**
     *根据展示类型查询
     * @param showTypeId
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public List<ShowAllVo> findShowByTypeId(Long showTypeId){
        return showMapper.findShowByTypeId(showTypeId);
    }
}




